<?php


namespace app\common\model;

use think\Model;

class Basemodel extends Model
{

    /**
     * 分页功能
     */
    public function page_index($datas = [], $order = null)
    {
        return $this->where($datas)
            ->order($order)
            ->paginate();
    }


//    /**
//     * 左边分类
//     * @param $data
//     * @param $pid
//     * @return string
//     */
//    public function json_right_nav($data, $pid = '0')
//    {
//        static $arr = array();
//
//        foreach ($data as $k => $v) {
//            if ($v['pid'] == $pid) {
//                $v['url'] = stripos($v['link'], "http") === 0 ? $v['link'] : url($v['link'] . '/index', ['navid'=>$v['id']]);
//                $v['chiid'] = $this->json_right_nav($data, $v['id']);
//                $arr[] = $v;
//            }
//        }
//        return $arr;
//    }


    /**
     * 删除
     * @param $id
     * @return int
     */
    public function del($id)
    {
        return $this->where('id', $id)->delete();
    }



    /**
     * 排除自己的uid
     */
    public function outpid($pid)
    {
        $data['lang'] = cookie('think_var');
        $re = $this->outsort($this->where($data)->order('id desc')->select(), $pid);
        return $this->sort($this->where($data)->whereNotIn('id', $re)->order('id desc')->select());
    }

    public function outsort($data, $pid = 0)
    {
        static $arr = array();
        $arr[] = $pid;

        foreach ($data as $k => $v) {
            if ($v['pid'] == $pid) {
                $this->outsort($data, $v['id']);
            }
        }
        return $arr;
    }


    /**
     * 上下页
     * @param $id   当前页面id
     * @param $datas    条件
     */
    public function getnext($id, $where = null)
    {
        $where['id'] = ['<', $id];
        $result ['pre'] = $this->where($where)->order('id desc')->cache(60)->find();
        $result ['pre'] = $result['pre'] ? $result['pre'] : '';

        $where['id'] = ['>', $id];
        $result ['next'] = $this->where($where)->order('id asc')->cache(60)->find();
        $result ['next'] = $result ['next'] ? $result ['next'] : '';

        return $result;
    }

}